package com.gexiaobao.pigeon.ui.auction.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentClosingAuctionBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterTomorrowEnd
import com.gexiaobao.pigeon.ui.auction.ActivityAuctionDetail
import com.gexiaobao.pigeon.ui.auction.ActivityAuctionPigeonDetail
import com.gexiaobao.pigeon.viewmodel.AuctionViewModel
import com.gexiaobao.pigeon.viewmodel.SharedViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/11/11 16:17
 * @Describe    : 明日结拍
 */
class FragmentTomorrowEnd : BaseFragment<AuctionViewModel, FragmentClosingAuctionBinding>() {

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>
    private val mAdapterTomorrow: AdapterTomorrowEnd by lazy { AdapterTomorrowEnd(arrayListOf()) }
    private var sharedViewModel: SharedViewModel? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        sharedViewModel = ViewModelProvider(requireActivity())[SharedViewModel::class.java]
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initView(savedInstanceState: Bundle?) {
        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRecyclerView) {
            //点击重试时触发的操作
            onRefresh()
        }

        mDatabind.swipeRecyclerView.init(LinearLayoutManager(context), mAdapterTomorrow)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 8f)))

        sharedViewModel?.dataTomorrow?.observe(viewLifecycleOwner) {
            if (it.isEmpty() || it == null) {
                mDatabind.llEmptyData.isVisible = true
                mDatabind.swipeRecyclerView.isVisible = false
            } else {
                mDatabind.llEmptyData.isVisible = false
                mDatabind.swipeRecyclerView.isVisible = true
                mAdapterTomorrow.setList(it)
                mAdapterTomorrow.notifyDataSetChanged()
            }
        }

        mAdapterTomorrow.setOnItemClickListener { _, _, position ->
            startActivity<ActivityAuctionPigeonDetail>("goodId" to mAdapterTomorrow.data[position].goodId,"type" to 1)
        }
    }

    private fun onRefresh() {
        loadsir.showLoading()
    }

    companion object {
        fun newInstance(): FragmentTomorrowEnd {
            val fragment = FragmentTomorrowEnd()
            return fragment
        }
    }

}